//---------------------------------------------------------------------------------------
GLOBAL

	STRUCT tseditor
	
		_point position;	// posicion del cursor en tiles
		
		layer;	// capa seleccionada
		tile;	// tile seleccionado
	
	END

END


//---------------------------------------------------------------------------------------
PROCESS tsEditor()
//
// Cursor Editor de mapas
//

PRIVATE
	
	_point last; // ultimo tile editado
	
	tile_min = 0;
	tile_max = 18;
	
	selected_tile;
	
	key_lock;
	click_lock;
	lock;
	
	i, j;
	
END

BEGIN

	file = load_fpg("fpg/gui.fpg");
	graph = 1;
	set_center(file,graph,0,0);
	
	write(0,SCREEN_X /2, SCREEN_Y-31, 7, "ARCHIVOS: [F5] Guardar");
	write(0,SCREEN_X /2, SCREEN_Y-21, 7, "CAPAS: [Q] Cambia - [W] limpia");
	write(0,SCREEN_X /2, SCREEN_Y-11, 7, "TILES: [A][S] Cambia seleccion");
	write(0,SCREEN_X /2, SCREEN_Y -1, 7, "EDICION: Mouse 1 Editar - Mouse2 Borrar");
	
	varMonitor(0, 40, "PX", &tseditor.position.x);
	varMonitor(50, 40, "PY", &tseditor.position.y);
	varMonitor(0, 50, "tseditor.layer", &tseditor.layer);
	varMonitor(0, 60, "Selected", &selected_tile);
	
	varMonitor(0, 70, "LX", &last.x);
	varMonitor(50, 70, "LY", &last.y);
	varMonitor(00, 80, "Lock", &lock);
	
	mouse.x = SCREEN_X / 2;
	mouse.y = SCREEN_Y / 2;

	LOOP
	
		tseditor.position.x = (ts.pos.x + mouse.x) / TILE_X;
		tseditor.position.y = (ts.pos.y + mouse.y) / TILE_Y;
	
		x = mouse.x;
		y = mouse.y;
		
		// cambia de capa
		IF ( key ( _q ) AND NOT key_lock)
			key_lock = true;
			
			tseditor.layer++;
			IF ( tseditor.layer > 1 )
				tseditor.layer = 0;
			END
			
		END
		
		// limpia la capa seleccionada
		IF ( key ( _w ) AND NOT key_lock )
		
			key_lock = true;
			ts.draw = true;
		
			FOR (i=0; i<TILES_X; i++)
				FOR (j=0; j<TILES_X; j++)
				
					IF ( tseditor.layer == 0 )
						map.base[i][j] = 0;
					ELSEIF ( tseditor.layer == 1 )
						map.over[i][j] = 0;
					END
					
				END
			END
		END
		
		// cambia de tile
		IF ( key (_s) AND NOT key_lock)
			key_lock = true;
			selected_tile++;
			IF ( selected_tile > tile_max )
				selected_tile = tile_min;
			END
		ELSEIF ( key ( _a ) AND NOT key_lock)
			key_lock = true;
			selected_tile--;
			IF ( selected_tile < tile_min )
				selected_tile = tile_max;
			END
			
		END
		
		// guarda el mapa
		IF ( key (_f5) AND NOT key_lock )
			key_lock = true;
			save("maps/map1.dat", map);
			
			print("MAPA GUARDADO");
			
		END
		
		IF ( NOT key ( _a ) AND NOT key ( _s ) AND NOT key ( _q ) AND NOT key ( _w ) AND NOT key ( _f5 ) AND key_lock )
			key_lock = false;
		END
		
		
		
		
		
		IF ( mouse.left AND NOT lock )
			
			print("Editar Tile");
		
			lock = true;
			tileEdit(tseditor.position, tseditor.layer, selected_tile);
			
			last = tseditor.position;
			
		ELSEIF ( mouse.right AND NOT lock )
			
			print("Borrar Tile");
		
			lock = true;
			tileEdit(tseditor.position, tseditor.layer, 0);
			
			last = tseditor.position;
			
		END
		
		IF ( last.x <> tseditor.position.x OR last.y <> tseditor.position.y OR ( mouse.left == false AND mouse.right == false ) )
			lock = false;
		END
		
		
		
	
		frame;
		
	END

END

//---------------------------------------------------------------------------------------
FUNCTION tileEdit(_point pos, int layer, int value)
//
// Edita un tile en el mapa
//

BEGIN

	SWITCH ( tseditor.layer )
	
		CASE 0:	// background
			map.base[pos.x][pos.y] = value;
		END
		
		CASE 1:
			map.over[pos.x][pos.y] = value;
		END
		
	END
	
	ts.draw = true;
	
	return 0;
	
END

//---------------------------------------------------------------------------------------
FUNCTION tsMapFill()
//
// Rellena el mapa con datos aleatorios
//

PRIVATE
	i; j;
END
BEGIN
	FOR (i=0; i<TILES_X; i++)
		FOR (j=0; j<TILES_X; j++)
		
			map.base[i][j] = rand(0,8);
			
			map.over[i][j] = rand(0,8);
			
		END
	END
	ts.draw = true;
	return 0;
END


